package kyssion.leetcode.num51_100;

import kyssion.leetcode.util.TreeNode;

public class code98_验证二叉搜索树 {

    //[10,5,15,null,null,6,20]
    public static void main(String[] args) {

    }

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        return ans(root, null, null);
    }

    private boolean ans(TreeNode root, TreeNode leftNode, TreeNode rightNode) {
        if (root == null) {
            return true;
        }

        if ((leftNode == null || root.val < leftNode.val) && (rightNode == null || (root.val > rightNode.val))) {
            return ans(root.left, root, rightNode) &&
                    ans(root.right, leftNode, root);
        } else {
            return false;
        }
    }
}
